/**
 * 
 */
package id.ac.gunadarma.paracorp.controller;

import java.util.Set;

import id.ac.gunadarma.paracorp.bean.CollectionNameBean;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.HtmlMacroComponent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabbox;
import org.zkoss.zul.Tabpanel;
import org.zkoss.zul.Tabpanels;
import org.zkoss.zul.Tabs;
import org.zkoss.zul.Window;

/**
 * @author rezalesmana
 *
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class AdminController extends SelectorComposer<Component> {
	
	private static final long serialVersionUID = 1L;
	
	@WireVariable
	private CollectionNameBean collectionNameBean;
	
	@Wire
	private Tabbox uiTabboxLanguagesAdmin;
	
	@Wire
	private Tabs uiTabs;
	
	@Wire
	private Tabpanels uiTabpanels;
	
	private Component adminComponent;
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		setAdminComponent(comp);
		Set<String> keySet = getCollectionNameBean().
				getCollectionNameMap().keySet();
		
		for(String key : keySet) {
			this.openTranslatedLanguageTab(key);
		}
	}

	@Listen("onClick = #uiUploadButton")
    public void showModal(Event e) {
        //create a window programmatically and use it as a modal dialog.
        Window window = (Window)Executions.createComponents(
                "/admin/modal_dialog/uploadsentences.zul", null, null);
        window.doModal();
    }

	public CollectionNameBean getCollectionNameBean() {
		return collectionNameBean;
	}

	public void setCollectionNameBean(CollectionNameBean collectionNameBean) {
		this.collectionNameBean = collectionNameBean;
	}

	public Tabbox getUiTabboxLanguagesAdmin() {
		return uiTabboxLanguagesAdmin;
	}

	public void setUiTabboxLanguagesAdmin(Tabbox uiTabboxLanguagesAdmin) {
		this.uiTabboxLanguagesAdmin = uiTabboxLanguagesAdmin;
	}

	public Tabs getUiTabs() {
		return uiTabs;
	}

	public void setUiTabs(Tabs uiTabs) {
		this.uiTabs = uiTabs;
	}

	public Tabpanels getUiTabpanels() {
		return uiTabpanels;
	}

	public void setUiTabpanels(Tabpanels uiTabpanels) {
		this.uiTabpanels = uiTabpanels;
	}
	
	public Component getAdminComponent() {
		return adminComponent;
	}

	public void setAdminComponent(Component adminComponent) {
		this.adminComponent = adminComponent;
	}

	private void openTranslatedLanguageTab(String key) {
		Tab tab = new Tab(key);
		this.getUiTabs().appendChild(tab);
		Tabpanel tabpanel = new Tabpanel();
		this.getUiTabpanels().appendChild(tabpanel);
		HtmlMacroComponent translatedMacro = (HtmlMacroComponent)
			   getUiTabboxLanguagesAdmin().getPage().
			   getComponentDefinition("translatedwindow", false).
			   newInstance(this.getPage(), null);
		translatedMacro.setParent(tabpanel);
		translatedMacro.applyProperties();
		translatedMacro.setDynamicProperty("language", 
				getCollectionNameBean().getCollectionNameMap().get(key));
		translatedMacro.afterCompose();
	}
	
}
